//
//  HCSanimationView.m
//  demoa
//
//  Created by WillyZhao on 2017/1/16.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCSanimationView.h"

#import "HCHomeNewsModel.h"

@interface HCSanimationView () {
    int count;
    int flag;                       //标识当前是哪个view显示
    NSTimer *timer_;                //定时器
    NSMutableArray *dataArr;
    HCHomeNewsModel *model_;
    NSInteger inde;
    NSInteger currentI;
}

@property (nonatomic, strong) UIView *currentView;          //当前显示的视图

@property (nonatomic, strong) UIView *hiddenView;           //即将隐藏的视图

@property (nonatomic,strong) UILabel *currentLabel;
@property (nonatomic,strong) UIButton *currentBtn;
@property (nonatomic,strong) UIButton *hidenBtn;
@property (nonatomic,strong) UILabel *hidenLabel;

@property (nonatomic,strong) UILabel *currentLabel_d;
@property (nonatomic,strong) UIButton *currentBtn_d;
@property (nonatomic,strong) UIButton *hidenBtn_d;
@property (nonatomic,strong) UILabel *hidenLabel_d;

@end

@implementation HCSanimationView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"happy_home_news"]];
        im.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        im.userInteractionEnabled = YES;
        [self addSubview:im];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, YBKAdaptiveW(79), self.frame.size.height);
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        [self initializeViews];
    }
    return self;
}

-(void)btnAction:(UIButton *)btn {
    if (self.block) {
        self.block();
    }
}

-(void)initializeViews {
    count = 0;
    flag = 0;
    
    self.layer.masksToBounds = YES;
    
    //创建定时器
    [self createTimer];
    
    //创建当前显示的view
    [self createCurrentView];
    
    //创建隐藏的view
    [self createHiddenView];
    
    //改进
    //    UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];
    //    [self addGestureRecognizer:longPress];
    //
    //    [tap requireGestureRecognizerToFail:longPress];
    
}

- (void)dealTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"11");
    if ([self.delegate respondsToSelector:@selector(animationViewClicked:)]&&self.delegate) {
        
        NSString *ss = [NSString stringWithFormat:@"%@",((UILabel *)[tap view]).text];
        
        for (int i = 0; i<model_.index.count; i++) {
            HCHomeNewsData *data3 = model_.index[i];
            if ([data3.title isEqualToString:ss]) {
                [self.delegate animationViewClicked:i];
            }
        }
    }
}

-(void)dealLongPress:(UILongPressGestureRecognizer*)longPress{
    
    if(longPress.state==UIGestureRecognizerStateEnded){
        
        timer_.fireDate=[NSDate distantPast];
        
        
    }
    if(longPress.state==UIGestureRecognizerStateBegan){
        
        timer_.fireDate=[NSDate distantFuture];
    }
    
    
    
    
}

-(void)createTimer {
    inde = 0;
    timer_ = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer_ forMode:NSRunLoopCommonModes];
}

-(void)dealTimer {
//    NSLog(@"当前的inde:%ld",inde);
    
    count++;
    
    if (count == dataArr.count) {
        count = 0;
    }
    //
    //    NSInteger i= model_.index.count/4;
    //    NSInteger j = model_.index.count%4;
    
    //    NSLog(@"得出--%ld 余数--%ld",i,j);
    
    if (flag == 1) {
        //赋值操作
        if (model_) {
            //            HCHomeNewsData *data = model_.index[flag];
            //            self.currentLabel.text = [NSString stringWithFormat:@"%@",data.title];
            HCHomeNewsData *data = model_.index[inde];
            self.currentLabel.text = [NSString stringWithFormat:@"%@",data.title];
            inde++;
            
            if (inde >= model_.index.count) {
                inde =0;
            }
            
            HCHomeNewsData *data1 = model_.index[inde];
            self.currentLabel_d.text = [NSString stringWithFormat:@"%@",data1.title];
            inde++;
            
            if (inde >= model_.index.count) {
                inde =0;
            }
        }
    }
    
    if (flag == 0) {
        //赋值操作
        if (model_) {
            
            HCHomeNewsData *data2 = model_.index[inde];
            self.hidenLabel.text = [NSString stringWithFormat:@"%@",data2.title];
            inde++;
            
            if (inde >= model_.index.count) {
                inde =0;
            }
            
            HCHomeNewsData *data3 = model_.index[inde];
            self.hidenLabel_d.text = [NSString stringWithFormat:@"%@",data3.title];
            inde++;
            
            if (inde >= model_.index.count) {
                inde =0;
            }
            
        }
    }
    
    currentI = inde;
    
    if (flag == 0) {
        [UIView animateWithDuration:.6f animations:^{
            self.currentView.frame = CGRectMake(YBKAdaptiveW(79), -self.frame.size.height, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            flag = 1;
            self.currentView.frame = CGRectMake(YBKAdaptiveW(79), self.frame.size.height, self.frame.size.width, self.frame.size.height);
        }];
        [UIView animateWithDuration:0.6 animations:^{
            self.hiddenView.frame = CGRectMake(YBKAdaptiveW(79), 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:0.6 animations:^{
            self.hiddenView.frame = CGRectMake(YBKAdaptiveW(79), -self.frame.size.height, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            flag = 0;
            self.hiddenView.frame = CGRectMake(YBKAdaptiveW(79), self.frame.size.height, self.frame.size.width, self.frame.size.width);
        }];
        [UIView animateWithDuration:0.6 animations:^{
            self.currentView.frame = CGRectMake(YBKAdaptiveW(79), 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            
        }];
    }
    
}


-(void)createCurrentView {
    self.currentView = [[UIView alloc]initWithFrame:CGRectMake(YBKAdaptiveW(79), 0, self.frame.size.width, self.frame.size.height)];
    self.currentView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.currentView];
    
    //此处是类型按钮(不需要点击)
    self.currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.currentBtn.frame = CGRectMake(10, 13, YBKAdaptiveW(30), YBKAdaptiveH(15));
    self.currentBtn.layer.masksToBounds = YES;
    self.currentBtn.layer.cornerRadius = 3;
    self.currentBtn.layer.borderWidth = 1;
    self.currentBtn.layer.borderColor = [UIColor redColor].CGColor;
    
    [self.currentBtn setTitle:[NSString stringWithFormat:@" 热议 "] forState:UIControlStateNormal];
    [self.currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.currentBtn.titleLabel.font = [UIFont systemFontOfSize:YBKAdaptiveFont(9)];
    [self.currentView addSubview:self.currentBtn];
    
    //此处是类型按钮(不需要点击)
    self.currentBtn_d = [UIButton buttonWithType:UIButtonTypeCustom];
    self.currentBtn_d.frame = CGRectMake(10, self.currentBtn.frame.origin.y+self.currentBtn.frame.size.height+5, YBKAdaptiveW(30), YBKAdaptiveH(15));
    self.currentBtn_d.layer.masksToBounds = YES;
    self.currentBtn_d.layer.cornerRadius = 3;
    self.currentBtn_d.layer.borderWidth = 1;
    self.currentBtn_d.layer.borderColor = [UIColor redColor].CGColor;
    
    [self.currentBtn_d setTitle:[NSString stringWithFormat:@" 头条 "] forState:UIControlStateNormal];
    [self.currentBtn_d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.currentBtn_d.titleLabel.font = [UIFont systemFontOfSize:YBKAdaptiveFont(9)];
    [self.currentView addSubview:self.currentBtn_d];
    
    //内容标题
    self.currentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+4, 0, self.currentView.frame.size.width-(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+10+10)-YBKAdaptiveW(79), self.currentView.frame.size.height/2.0)];
    self.currentLabel.text = @"莫名其妙的所有事情都是这样,都会懂爱搜我骄傲你";
    self.currentLabel.textAlignment = NSTextAlignmentLeft;
    self.currentLabel.textColor = [UIColor blackColor];
    self.currentLabel.font = [UIFont systemFontOfSize:12];
    [self.currentView addSubview:self.currentLabel];
    self.currentLabel.center = CGPointMake(self.currentLabel.center.x, self.currentBtn.center.y);
    
    
    //内容标题
    self.currentLabel_d = [[UILabel alloc]initWithFrame:CGRectMake(self.currentBtn_d.frame.origin.x+self.currentBtn_d.frame.size.width+4, 0, self.currentView.frame.size.width-(self.currentBtn_d.frame.origin.x+self.currentBtn_d.frame.size.width+10+10)-YBKAdaptiveW(79), self.currentView.frame.size.height/2.0)];
    self.currentLabel_d.text = @"UI东南东安山东那Doin奥ID奥年度情浓";
    self.currentLabel_d.textAlignment = NSTextAlignmentLeft;
    self.currentLabel_d.textColor = [UIColor blackColor];
    self.currentLabel_d.font = [UIFont systemFontOfSize:12];
    [self.currentView addSubview:self.currentLabel_d];
    self.currentLabel_d.center = CGPointMake(self.currentLabel_d.center.x, self.currentBtn_d.center.y);
    
    self.currentLabel.userInteractionEnabled= YES;
    self.currentLabel_d.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
    [self.currentLabel addGestureRecognizer:tap];
    
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
    [self.currentLabel_d addGestureRecognizer:tap1];
    
    //    self.currentLabel.backgroundColor = [UIColor redColor];
    //    self.currentLabel_d.backgroundColor = [UIColor blueColor];
    
    
}

-(void)createHiddenView {
    self.hiddenView = [[UIView alloc]initWithFrame:CGRectMake(YBKAdaptiveW(79), self.frame.size.height, self.frame.size.width, self.frame.size.height)];
    self.hiddenView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.hiddenView];
    
    //此处是类型按钮(不需要点击)
    self.hidenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.hidenBtn.frame = CGRectMake(10, 13, YBKAdaptiveW(30), YBKAdaptiveH(15));
    self.hidenBtn.layer.masksToBounds = YES;
    self.hidenBtn.layer.cornerRadius = 3;
    self.hidenBtn.layer.borderWidth = 1;
    self.hidenBtn.layer.borderColor = [UIColor redColor].CGColor;
    [self.hidenBtn setTitle:@" 热议 " forState:UIControlStateNormal];
    [self.hidenBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.hidenBtn.titleLabel.font = [UIFont systemFontOfSize:YBKAdaptiveFont(9)];
    [self.hiddenView addSubview:self.hidenBtn];
    
    //此处是类型按钮(不需要点击)
    self.hidenBtn_d = [UIButton buttonWithType:UIButtonTypeCustom];
    self.hidenBtn_d.frame = CGRectMake(10, self.hidenBtn.frame.origin.y+self.hidenBtn.frame.size.height+5, YBKAdaptiveW(30), YBKAdaptiveH(15));
    self.hidenBtn_d.layer.masksToBounds = YES;
    self.hidenBtn_d.layer.cornerRadius = 3;
    self.hidenBtn_d.layer.borderWidth = 1;
    self.hidenBtn_d.layer.borderColor = [UIColor redColor].CGColor;
    [self.hidenBtn_d setTitle:@" 力荐 " forState:UIControlStateNormal];
    [self.hidenBtn_d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.hidenBtn_d.titleLabel.font = [UIFont systemFontOfSize:YBKAdaptiveFont(9)];
    [self.hiddenView addSubview:self.hidenBtn_d];
    
    //内容标题
    self.hidenLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+4, 0, self.hiddenView.frame.size.width-(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+10+10) - YBKAdaptiveW(79), self.hiddenView.frame.size.height/2.0)];
    self.hidenLabel.text = @"断奶偶倒我就调动小鸟大哦哦呢, 大大咧咧你.";
    self.hidenLabel.textAlignment = NSTextAlignmentLeft;
    self.hidenLabel.textColor = [UIColor blackColor];
    self.hidenLabel.font = [UIFont systemFontOfSize:12];
    [self.hiddenView addSubview:self.hidenLabel];
    self.hidenLabel.center = CGPointMake(self.hidenLabel.center.x, self.hidenBtn.center.y);
    
    //内容标题
    self.hidenLabel_d = [[UILabel alloc]initWithFrame:CGRectMake(self.hidenBtn_d.frame.origin.x+self.hidenBtn_d.frame.size.width+4, 0, self.hiddenView.frame.size.width-(self.hidenBtn_d.frame.origin.x+self.hidenBtn_d.frame.size.width+10+10) - YBKAdaptiveW(79), self.hiddenView.frame.size.height/2.0)];
    self.hidenLabel_d.text = @"奥迪R8，买了你就发。不要，别不信。虽然那就是吹牛。";
    self.hidenLabel_d.textAlignment = NSTextAlignmentLeft;
    self.hidenLabel_d.textColor = [UIColor blackColor];
    self.hidenLabel_d.font = [UIFont systemFontOfSize:12];
    [self.hiddenView addSubview:self.hidenLabel_d];
    self.hidenLabel_d.center = CGPointMake(self.hidenLabel_d.center.x, self.hidenBtn_d.center.y);
    
    self.hidenLabel.userInteractionEnabled = YES;
    self.hidenLabel_d.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
    [self.hidenLabel addGestureRecognizer:tap];
    
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
    [self.hidenLabel_d addGestureRecognizer:tap1];
    
    //
    //    self.hidenLabel.backgroundColor = [UIColor redColor];
    //    self.hidenLabel_d.backgroundColor = [UIColor blueColor];
}

-(void)setModel:(id)model {
    model_ = (HCHomeNewsModel *)model;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
